home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
53142
/
53142.xpi
/
content
/
options.js
< prev
next >
Wrap
Text File
|
2009-12-07
|
12KB
|
331 lines
/*##########################################################################
Copyright 2009 Tim Reid
This file is part of the Stack Overflow Reputation Display extension
for Mozilla Firefox.
Stack Overflow Reputation Display is free software: you can
redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation, either
version 3 of the License, or (at your option) any later version.
Stack Overflow Reputation Display is distributed in the hope that it
will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public
License along with Stack Overflow Reputation Display. If not,
see <http://www.gnu.org/licenses/>.
##########################################################################*/
var sorepoptions = {
interfaces: {
prefs: Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService)
.getBranch("extensions.sorepdisplay."),
console: Components.classes["@mozilla.org/consoleservice;1"]
.getService(Components.interfaces.nsIConsoleService),
},
doCancel: function () {
window.close();
},
doOK: function () {
var configs = [];
for (var i=0; i<this.accounts.length; i++)
configs.push(this.accounts[i].config);
this.interfaces.prefs.setCharPref("config", configs.join(","));
if (this.reputationtextcolor != this.interfaces.prefs.getCharPref("reputationtextcolor"))
this.interfaces.prefs.setCharPref("reputationtextcolor", this.reputationtextcolor);
if (this.badgetextcolor != this.interfaces.prefs.getCharPref("badgetextcolor"))
this.interfaces.prefs.setCharPref("badgetextcolor", this.badgetextcolor);
},
nsResolver: function (prefix) {
var ns = {
'xul': 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul'
};
return ns[prefix] || null;
},
menuselect: function (menu, attribute, value) {
var result = menu.ownerDocument
.evaluate('.//xul:menuitem[@' + attribute + '="' + value + '"]',
menu,
this.nsResolver,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null);
if (result && result.singleNodeValue) {
menu.selectedItem = result.singleNodeValue;
return true;
}
return false;
},
select: function (e) {
var index = e.target.selectedIndex;
this.selectedaccount = this.accounts[index];
this.updatedisplay();
},
updatesite: function () {
var sitemenu = document.getElementById("site");
this.menuselect(sitemenu, "value", this.selectedaccount.site.sitetag);
},
updateuser: function () {
var userlabel = document.getElementById("userlabel");
var clearbutton = document.getElementById("clear");
clearbutton.setAttribute("disabled", "false");
var name = this.selectedaccount.name;
var user = this.selectedaccount.user;
var text;
userlabel.setAttribute("disabled", "false");
if (user && name) {
text = name + " (" + user + ")";
} else if (user) {
text = user;
} else {
text = "login to website to set user";
clearbutton.setAttribute("disabled", "true");
userlabel.setAttribute("disabled", "true");
}
userlabel.setAttribute("value", text);
},
updateinterval: function () {
var intervalmenu = document.getElementById("interval");
while (intervalmenu.firstChild)
intervalmenu.removeChild(intervalmenu.firstChild);
var p = document.createElement("menupopup");
intervalmenu.appendChild(p);
for (var i=0; i<this.selectedaccount.site.intervals.length; i++) {
var mi = document.createElement("menuitem");
mi.setAttribute("value", this.selectedaccount.site.intervals[i].value);
mi.setAttribute("label", this.selectedaccount.site.intervals[i].description);
p.appendChild(mi);
}
this.menuselect(intervalmenu, "value", "900");
this.menuselect(intervalmenu, "label", intervalmenu.getAttribute("label"));
this.menuselect(intervalmenu, "value", this.selectedaccount.updateinterval);
},
updateaction: function () {
var actionmenu = document.getElementById("clickaction");
while (actionmenu.firstChild)
actionmenu.removeChild(actionmenu.firstChild);
var p = document.createElement("menupopup");
actionmenu.appendChild(p);
for (var i=0; i<this.selectedaccount.site.bookmarks.length; i++) {
var mi = document.createElement("menuitem");
mi.setAttribute("value", this.selectedaccount.site.bookmarks[i]);
mi.setAttribute("label", this.selectedaccount.site
.pages[this.selectedaccount.site
.bookmarks[i]].description);
p.appendChild(mi);
}
this.menuselect(actionmenu, "label", actionmenu.getAttribute("label"));
this.menuselect(actionmenu, "value", this.selectedaccount.clickaction);
},
updatedosounds: function () {
var dosoundsbox = document.getElementById("dosounds");
if (this.selectedaccount.dosounds)
dosoundsbox.setAttribute("checked", "true");
else
dosoundsbox.removeAttribute("checked");
},
updatedisplay: function () {
this.updatesite();
this.updateuser();
this.updateinterval();
this.updateaction();
this.updatedosounds();
},
siteselected: function (e) {
this.selectedaccount.site = new SOSite(e.target.value);
var listbox = document.getElementById("listbox");
var i = listbox.selectedIndex;
this.filllistbox();
listbox.selectedIndex = i;
},
intervalselected: function (e) {
this.selectedaccount.updateinterval = parseInt(e.target.value);
},
actionselected: function (e) {
this.selectedaccount.clickaction = e.target.value;
},
soundselected: function (e) {
this.selectedaccount.dosounds = e.target.checked;
},
clearaction: function (e) {
this.selectedaccount.user = null;
this.updateuser(this.selectedaccount);
},
deleteaction: function (e) {
var listbox = document.getElementById("listbox");
var i = listbox.selectedIndex;
if (i >= 0) {
this.accounts.splice(i, 1);
this.filllistbox();
if (this.accounts.length >= i+1)
listbox.selectedIndex = i;
else
listbox.selectedIndex = this.accounts.length - 1;
}
},
addaction: function (e) {
this.accounts.push(new SOAccount("so"));
this.filllistbox();
var listbox = document.getElementById("listbox");
listbox.selectedIndex = this.accounts.length - 1;
},
repcoloraction: function (e) {
if (e.target.style.backgroundColor) {
this.interfaces
.console
.logStringMessage("SORepdisplay options reputation score text colour selected: " + e.target.style.backgroundColor);
document.getElementById("repcolorpicker-mp").hidePopup();
this.reputationtextcolor = e.target.style.backgroundColor;
this.reptextcolorswatch.style.backgroundColor = this.reputationtextcolor;
}
},
badgecoloraction: function (e) {
if (e.target.style.backgroundColor) {
this.interfaces
.console
.logStringMessage("SORepdisplay options badge count text colour selected: " + e.target.style.backgroundColor);
document.getElementById("badgecolorpicker-mp").hidePopup();
this.badgetextcolor = e.target.style.backgroundColor;
this.badgetextcolorswatch.style.backgroundColor = this.badgetextcolor;
}
},
filllistbox: function () {
var listbox = document.getElementById("listbox");
while (listbox.firstChild)
listbox.removeChild(listbox.firstChild);
for (var i=0; i<this.accounts.length; i++) {
var r = document.createElement("richlistitem");
var m = document.createElement("image");
m.setAttribute("height", 16);
m.setAttribute("width", 16);
m.setAttribute("src", this.accounts[i].site.smallicon);
r.appendChild(m);
var l = document.createElement("label");
l.setAttribute("flex", 1);
l.setAttribute("crop", "end");
l.setAttribute("value", this.accounts[i].site.title);
r.appendChild(l);
listbox.appendChild(r);
}
},
fillsitebox: function () {
var sitebox = document.getElementById("site");
while (sitebox.firstChild)
sitebox.removeChild(sitebox.firstChild);
var p = document.createElement("menupopup");
sitebox.appendChild(p);
for (var i=0; i<this.sites.length; i++) {
var mi = document.createElement("menuitem");
mi.setAttribute("value", this.sites[i].tag);
mi.setAttribute("label", this.sites[i].title);
p.appendChild(mi);
}
},
init: function () {
var me = this;
var config = this.interfaces.prefs.getCharPref("config");
var configparts = config.split(/,/);
var accounts = [];
for (var i=0; i<configparts.length; i++) {
if (configparts[i][0] == "#")
continue;
var account = new SOAccount(configparts[i]);
accounts.push(account);
}
this.accounts = accounts;
this.sites = [];
for (var i=0; i<SOSite.siteinfo.length; i+=2)
this.sites.push({
tag: SOSite.siteinfo[i],
title: SOSite.siteinfo[i+1].title,
});
this.filllistbox();
this.fillsitebox();
var listbox = document.getElementById("listbox");
listbox.addEventListener("select", function (e) { me.select(e); }, true);
listbox.selectedIndex = 0;
var siteselector = document.getElementById("site");
siteselector.addEventListener("command", function (e) { me.siteselected(e); }, true);
var intervalselector = document.getElementById("interval");
intervalselector.addEventListener("command", function (e) { me.intervalselected(e); }, true);
var actionselector = document.getElementById("clickaction");
actionselector.addEventListener("command", function (e) { me.actionselected(e); }, true);
var soundselector = document.getElementById("dosounds");
soundselector.addEventListener("command", function (e) { me.soundselected(e); }, true);
var clearbutton = document.getElementById("clear");
clearbutton.addEventListener("command", function (e) { me.clearaction(e); }, true);
var addbutton = document.getElementById("add");
addbutton.addEventListener("command", function (e) { me.addaction(e); }, true);
var deletebutton = document.getElementById("delete");
deletebutton.addEventListener("command", function (e) { me.deleteaction(e); }, true);
this.reputationtextcolor = this.interfaces.prefs.getCharPref("reputationtextcolor");
this.reptextcolorswatch = document.getElementById("repcolorpicker-hb");
this.reptextcolorswatch.style.backgroundColor = this.reputationtextcolor;
this.badgetextcolor = this.interfaces.prefs.getCharPref("badgetextcolor");
this.badgetextcolorswatch = document.getElementById("badgecolorpicker-hb");
this.badgetextcolorswatch.style.backgroundColor = this.badgetextcolor;
var repmp = document.getElementById("repcolorpicker-mp");
repmp.addEventListener("click", function (e) { me.repcoloraction(e); }, true);
var badgemp = document.getElementById("badgecolorpicker-mp");
badgemp.addEventListener("click", function (e) { me.badgecoloraction(e); }, true);
},
null: null
};
window.addEventListener("load", function (e) { sorepoptions.init(); }, false);